package marek.barak.classes;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import marek.barak.interfaces.UsersDAO;

public class UsersHddDAO implements UsersDAO {
	private List<User> list;
	private int curent;
	private Connection connection;
	public UsersHddDAO() throws IOException,ClassNotFoundException{
		connection = Connection.getInstance();
		list = connection.loadUsers();
	}
	
	@Override
	public User getCurentUser()throws IndexOutOfBoundsException, IOException, ClassNotFoundException {
		curent = connection.loadCurentUser();
		if(curent!=-1){
		if(list.size()>curent)
		return list.get(curent);
		}
		return new User("Nobody","");
		
	}


	@Override
	public List<User> getAllUsers() {
		// TODO Auto-generated method stub
		return list;
	}

	@Override
	public void addUser(User u) throws IOException {
		if(!list.contains(u))
		list.add(u);
		connection.saveUsers(list);
	}

	@Override
	public void removeUser(User u) {
		if(list.contains(u)){
			list.remove(u);
		}

	}

	@Override
	public void setCurentUser(User u) throws FileNotFoundException, IOException {
		curent = list.indexOf(u);
		connection.saveCurentUser(curent);
		
	}

}
